﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Guru.Web.UI;
using Guru.Utilities.Cryptography;
using HSM.Business;
using Guru.Web.Controls;
using Guru.Web.Controls.Telerik;
using Telerik.Web.UI;
using Guru.Utilities;

/// <summary>
/// Summary description for HSMPage
/// </summary>
public class HSMPage : GuruPage
{
    #region Properties
    public int FixedModalWidth
    {
        set;
        get;
    }
    public GuruAjaxManager AjaxManager
    {
        get 
        {
            return GuruAjaxManager.GetCurrent(this.Page) as GuruAjaxManager;
        }
    }
    #endregion

    protected void GetCurrentTheme()
    {
        if (HSMSessionManager.CurrentTheme == null)
        {
            if (Request.Url.ToString().ToLower().Contains("Default.aspx?id") &&
                !string.IsNullOrEmpty(Request["id"]) &&
                !string.IsNullOrEmpty(Request["theme"])
                )
            {
                HSMSessionManager.CurrentClientID = int.Parse(Request["id"]);
                HSMSessionManager.CurrentTheme = Request["theme"];
            }

        }
        this.Theme = HSMSessionManager.CurrentTheme;
    }

    #region Response Script
    public void AddAjaxResponseScript(string script)
    {
        RadAjaxManager ajaxManager = RadAjaxManager.GetCurrent(Page);
        if (ajaxManager != null)
            ajaxManager.ResponseScripts.Add(script);
    }   
    public void AddClientScript(string script, string key)
    {        
        Page.ClientScript.RegisterStartupScript(typeof(HSMPage), key, script, true);
    }
    public void AlertClient(string message)
    {
        AddClientScript(string.Format("alert('{0}');", message.ToJString()),Guid.NewGuid().ToString());
    }
    public void AlertAjaxClient(string message)
    {
        AddAjaxResponseScript(string.Format("alert('{0}');", message.ToJString()));
    }
    #endregion

    #region Modal Helper Methods
    public void CloseModal(int index)
    {
        string script =string.Empty;
        if (index <= 1) script = "$(document).ready(function(){ ClosePopupModal();});";
        else script = "$(document).ready(function(){ parent.ClosePopupModal(" + index + ");});";

        Page.ClientScript.RegisterStartupScript(typeof(HSMPage), "ClosePopupModal", script, true);
    }
    public void CloseModal(int index, string additionScript)
    {
        if (!string.IsNullOrEmpty(additionScript) && !additionScript.Trim().EndsWith(";")) additionScript += ";";

        string script = string.Empty;
        if (index <= 1)
        {
            script = "$(document).ready(function(){" + additionScript + @" 
                        ClosePopupModal();});";
        }
        else
        {
            script = "$(document).ready(function(){ " + additionScript + @" 
                    parent.ClosePopupModal(" + index + ");});";
        }

        Page.ClientScript.RegisterStartupScript(typeof(HSMPage), "ClosePopupModal", script, true);
    }
    public void CloseModalAjax(int index)
    {
        string script = string.Empty;
        if (index <= 1) script = "ClosePopupModal();";
        else script = "parent.ClosePopupModal(" + index + ");";

        ((IMasterPage)Page.Master).AjaxAddResponseScript(script);
    }
    public string BuildCloseModalScript(int index)
    {
        string script = string.Empty;
        if (index <= 1) script = "ClosePopupModal();";
        else script = "parent.ClosePopupModal(" + index + ");";

        return script;
    }

    public void OpenModal(int index, string link)
    {
        string script = string.Empty;
        if (index <= 1) script = "$(document).ready(function(){OpenPopupModal(1,'" + link + "');});";
        else script = "$(document).ready(function(){ parent.OpenPopupModal(" + index + ",'" + link + "');});";

        Page.ClientScript.RegisterStartupScript(typeof(HSMPage), "ClosePopupModal", script, true);
    }
    public void OpenModalAjax(int index, string link)
    {
        string script = string.Empty;
        if (index <= 1) script = "OpenPopupModal(1,'" + link + "');";
        else script = "parent.OpenPopupModal(" + index + ",'" + link + "');";

        ((IMasterPage)Page.Master).AjaxAddResponseScript(script);
    }
    public string BuildOpenModalScript(int index, string link)
    {
        string script = string.Empty;
        if (index <= 1) script = "OpenPopupModal(1,'" + link + "');";
        else script = "parent.OpenPopupModal(" + index + ",'" + link + "');";
        return script;
    }
    #endregion

    #region Override Framwwork
    protected override void OnPagePreInit()
    {
        GetCurrentTheme();
    }
    protected override void OnPageInit() 
    {
       
    }
    protected override void OnAspxWriteLog() { }
    protected override void OnIntruderWriteLog() { }
    protected override void DoIntruderDetected(IntruderType type) { }

    protected override void OnPreRender(EventArgs e)
    {
        base.OnPreRender(e);
        this.Page.Title = new TextResourceManager(this.CurrentLanguage).GetGlobalText("SystemTitle");

        if (this.FixedModalWidth > 0)
        {
            Guru.Web.Controls.Asp.Literal cssWidth = new Guru.Web.Controls.Asp.Literal();
            cssWidth.Text = "<style>.ModalMainContent{ width:" + this.FixedModalWidth + "px;}</style>";
            this.Page.Header.Controls.Add(cssWidth);
        }
    }
    #endregion
}
